package dao

import (
	"a-app/domain/entity"
	"sync"
)

var (
	appLoginDaoTemp *AppLoginDao
	appLoginDaoOnce sync.Once
)

type AppLoginDao struct{}

func NewLoginDao() *AppLoginDao {
	appLoginDaoOnce.Do(func() {
		appLoginDaoTemp = &AppLoginDao{}
	})
	return appLoginDaoTemp
}

func (m *AppLoginDao) GetUserByNameAndPwd(userName string, pwd string) entity.AppUserVo {
	var vUser entity.AppUserVo
	_, err := pgCli.Table(&entity.UserDto{}).Where("user_name=? and password=?", userName, pwd).Get(&vUser)
	if err != nil {
		return entity.AppUserVo{}
	}
	return vUser
}

func (m *AppLoginDao) GetUserByAccount(userName string) (entity.AppUser, error) {
	var userPo entity.AppUser
	_, err := pgCli.Table(&entity.AppUser{}).Where("user_name=?", userName).Get(&userPo)
	return userPo, err
}

func (m *AppLoginDao) AddUser(entity *entity.UserDto) (int64, error) {
	return pgCli.InsertOne(&entity)
}

func (m *AppLoginDao) Update(entity *entity.UserDto) (int64, error) {
	return pgCli.Table(&entity).Update(entity)
}
